package com.rfs.jvm;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author renfushuai
 * @date 2022/9/22
 */
public class SafePointTest {
    public static AtomicInteger num=new AtomicInteger(0);
    public static void main(String[] args) throws InterruptedException {
        // i 是int类型，属于可数循环，jvm不会设置安全点，所以会等主线程运行完成
        // 如果i设置成long 属于不可数循环，jvm会设置安全点，有安全点才会gc，主线程就不会等待其他线程完成
       Runnable runnable=()->{
           for (long i = 0; i < 1000000000; i++) {
               num.getAndIncrement();
          /*     if (i % 1000 == 0) {
                   try {
                       Thread.sleep(0);
                   } catch (InterruptedException e) {
                       throw new RuntimeException(e);
                   }
               }*/
           }
           System.out.println(Thread.currentThread().getName()+"执行结果");
       };
       Thread t1=new Thread(runnable);
       Thread t2=new Thread(runnable);
       t1.start();
       t2.start();
       Thread.sleep(1000);
        System.out.println("num="+num);
    }
}
